import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import vue from '@vitejs/plugin-vue';

export default defineConfig({
    plugins: [
        laravel({
            input: 'resources/js/app.js',
            ssr: 'resources/js/ssr.js',
            refresh: true,
        }),
        vue({
            template: {
                transformAssetUrls: {
                    base: null,
                    includeAbsolute: false,
                },
            },
        }),
    ],
    
    // 性能优化配置
    build: {
        // 代码分割策略
        rollupOptions: {
            output: {
                manualChunks(id) {
                    // 第三方库单独打包
                    if (id.includes('node_modules')) {
                        if (id.includes('vue')) {
                            return 'vendor-vue';
                        }
                        if (id.includes('@inertiajs')) {
                            return 'vendor-inertia';
                        }
                        if (id.includes('@headlessui') || id.includes('@heroicons')) {
                            return 'vendor-ui';
                        }
                        return 'vendor';
                    }
                }
            }
        },
        // 分块大小警告限制
        chunkSizeWarningLimit: 600,
        // 资源内联限制 (4KB以下内联为base64)
        assetsInlineLimit: 4096,
        // CSS代码分割
        cssCodeSplit: true,
        // 压缩选项
        minify: 'terser',
        terserOptions: {
            compress: {
                drop_console: true, // 生产环境移除console
                drop_debugger: true
            }
        }
    },
    
    // 优化依赖预构建
    optimizeDeps: {
        include: [
            'vue',
            '@inertiajs/vue3',
            '@headlessui/vue',
            '@heroicons/vue/24/outline',
            'ziggy-js'
        ]
    }
});
